<?php
require_once 'application/library/view/gcPager.php';
require_once 'application/library/helper/amountHelper.php';
require_once '../application/library/cache/gcSession.php';
require_once 'application/library/controller/gcBasicAction.php';
require_once 'application/library/service/commService/gcPageSizeService.php';

/**
 * gcLBasicAction
 * @author    Soochow University
 * @version   1.0
 * @exclude   作  成  日：2011/04/07
 */
class  gcLBasicAction extends gcBasicAction{

    /**
     * @see application/library/controller/gcBasicAction::init()
     */
    public function init(){
        parent::init();
        $strUserId = gcSession::getSession("userId");//管理员ID
        $strTypeFg = gcSession::getSession("userType");//管理员类型
        $strAdminName = null;
        switch ($strTypeFg) {
            case "1" :
                $strAdminName = "电子元件管理员";
                break;
            case "2" :
                $strAdminName = "辅助系统管理员";
                break;
            case "4" :
                $strAdminName = "超级管理员";
                break;
        }
        $this->gcView->adminName = $strAdminName;
        $this->gcView->adminId   = $strUserId;
    }

    /**
     * 翻页控件标签设定
     * setPagerLabel
     */
    private function setPagerLabel(){
        $this->gcView->pagerItem    = $this->gcTranslate->translate("pagerItem");
        $this->gcView->allPage      = $this->gcTranslate->translate("allPage");
        $this->gcView->pagerPage    = $this->gcTranslate->translate("pagerPage");
        $this->gcView->orderASC     = $this->gcTranslate->translate("orderASC");
        $this->gcView->orderDES     = $this->gcTranslate->translate("orderDES");
        $this->gcView->pageSizeItem = $this->gcTranslate->translate("pageSizeItem");
    }

    /**
     * 翻页控件用参数
     * getPagerParam
     * @param $dataCount
     * @return gcPager
     */
    protected  function getPagerParam($dataCount, $pageSizeId=null){
        $curPage=$this->getParam("curPage");
        $pageSize=$this->getParam("pageSize");

        if($pageSize == null){
            if($pageSizeId == null){
                $pageSizeId = pageSizeGroup::PAGE_SIZE_DEFAULT;
            }
            $gcPageSizeService = new gcPageSizeService();
            $arrPageSize = $gcPageSizeService->getPageSizeInfo($pageSizeId);
            $pageSize=$arrPageSize[0]["KEY_CODE"];
        }
        if ($curPage == 0) {
            $offset = 0;
        } else {
            $offset=($curPage-1) * $pageSize;
        }
        if($offset>=$dataCount){
            if($dataCount == 0){
                $curPage = 0;
                $offset = 0;
            }else if($dataCount%$pageSize == 0){
                $curPage = $dataCount/$pageSize;
                $offset = $dataCount - $pageSize;
            }else{
                $curPage = amountHelper::numberCut($dataCount/$pageSize) + 1;
                $offset=$dataCount-$dataCount%$pageSize;
            }
        }

        $pager=array("curPage"  => $curPage,
                     "Limit"    => $pageSize,
                     "Offset"   => $offset,
                     "pageSize" => $pageSize);

        return new gcPager($pager);
    }

    /**
     * 翻页空间初始化
     * pagerInit
     * @param $dataCount
     * @param $curPage
     * @param $pageIndex
     * @return gcPager
     */
    protected function pagerInit($dataCount,$curPage=1,$pageSize=null,$pageSizeId=null,$pageSizeShow=display::HIDE){
        $this->setPagerLabel();
        $gcPageSizeService = new gcPageSizeService();
        if($pageSizeId == null){
            $pageSizeId = pageSizeGroup::PAGE_SIZE_DEFAULT;
        }
        $arrPageSize = $gcPageSizeService->getPageSizeInfo($pageSizeId);
        if($pageSize == null){
            $pageSize=$arrPageSize[0]["KEY_CODE"];
        }
        
        if ($curPage == 0) {
            $offset = 0;
        } else {
            $offset=($curPage-1) * $pageSize;
        }
        if($offset>$dataCount){
            $offset=$dataCount-$dataCount%$pageSize;
        }

        $pager=array("Limit"   => $pageSize,
                     "Offset"  => $offset,
                     "curPage"  => $curPage,
                     "pageSize" => $pageSize);

        if($dataCount == 0){
            $this->gcView->fromRecord=0;
        }else{
            $this->gcView->fromRecord=($curPage-1)*$pageSize+1;
        }
        if(($dataCount-$curPage*$pageSize) >= 0){
            $this->gcView->toRecord=$curPage * $pageSize;
        }else{
            $this->gcView->toRecord=$dataCount;
        }
        $this->gcView->arrPageSize = $arrPageSize;
        if(count($arrPageSize)== 1){
            $pageSizeShow = display::HIDE;
        }
        $this->gcView->pageSizeShow = $pageSizeShow;
        $this->gcView->pageSize  = $pageSize;
        $this->gcView->totalPage=ceil($dataCount/$pageSize);
        if($dataCount == 0){
            $this->gcView->curPage=0;
        }else{
            $this->gcView->curPage=$curPage;
        }
        $this->gcView->dataCount=$dataCount;
        return new gcPager($pager);
    }
    
    protected function initMenu($strMenuType) {
        $arrMenu = array();
        $strHost = "http://localhost/EPSite/public/";
        switch ($strMenuType) {
            case "1":
                $arrMenu = array(array("NAME" => "库存查询", "HREF" => $strHost . "goods/goodsList/index"),
                                 array("NAME" => "新增入库", "HREF" => $strHost . "goods/goodsAddEdit/index"),
                                 array("NAME" => "出库更新", "HREF" => $strHost . "supplier/supplierlist/index"),
                                 array("NAME" => "库存预警", "HREF" => $strHost . "stock/Stockalertlist/index"),
                                 array("NAME" => "电子元件配方", "HREF" => $strHost . "goods/goodsFormulaList/index"));
                break;
            case "2":
                $arrMenu = array(array("NAME" => "供应商查询", "HREF" => $strHost . "supplier/supplierList/index"),
                                 array("NAME" => "新增供应商", "HREF" => $strHost . "supplier/supplierEdit/index"));
                break;
            case "3":
                $arrMenu = array(array("NAME" => "订单查询", "HREF" => $strHost . "/order/Orderlist/index"),
                                 array("NAME" => "新增订单", "HREF" => $strHost . "order/Orderedit/index"));
                break;
            case "4":
                $arrMenu = array(array("NAME" => "站点信息设置", "HREF" => $strHost . "site/siteInfoSet/index"),
                                 array("NAME" => "资讯新闻管理", "HREF" => $strHost . "site/newsList/index"));
                break;
            case "5":
                $strUserId = gcSession::getSession("userId");
                $arrMenu = array(array("NAME" => "用户详细信息", "HREF" => $strHost . "user/userView/index/UserId/" . $strUserId),
                                 array("NAME" => "用户一览", "HREF" => $strHost . "user/userList/index"),
                                 array("NAME" => "权限管理", "HREF" => $strHost . "user/permissionEdit/index/"),
                                 array("NAME" => "变更密码", "HREF" => $strHost . "user/changePsw/index/UserId/" . $strUserId));
                break;
        }
        $this->gcView->arrMenu = $arrMenu;
        
    }
    
    protected function boolUserTypeCheck($strNeededUserType) {
        $strUserId = gcSession::getSession("userId");
        $strUserType = gcSession::getSession("userType");
        
        $boolCheck = true;
        if (empty($strUserId)) {
            $boolCheck = false;
        } else {
            if ($strNeededUserType == "all") {
                $boolCheck = true;
            } else {
                if ($strUserType == "4") {
                    $boolCheck = true;
                } else {
                    if ($strUserType != $strNeededUserType) {
                        $boolCheck = false;
                    } else {
                        $boolCheck = true;
                    } 
                }
            }
        }
        if (!$boolCheck) {
            $gcSmarty = new gcViewSmarty("../application/modules/site/views");
            $this->gcView->setTemplatePath("../application/modules/site/views/templates");
            $this->gcView->render("err.tpl");
            exit;
        }
    }

}

